package com.dc.refactoring._4_简化条件表达式._8_IntroduceAssertion;

import com.google.common.base.Preconditions;

import java.util.Objects;

public class Employee {

    public static final double NULL_EXPENSE = -1.0;
    /**
     * 费用限额
     */
    private double expenseLimit = NULL_EXPENSE;
    /**
     * 主要项目
     */
    private Project primaryProject;

    /**
     * 获取费用限制
     * 当 员工费用限额不为空时，返回员工限额，否则返回 项目费用限额
     */
    double getExpenseLimit(){
        return (expenseLimit != NULL_EXPENSE) ?
                expenseLimit :
                primaryProject.getMemberExpenseLimit();
    }


    /**
     * 添加断言
     */
    double getExpenseLimit2(){

        // 添加断言，可以提前预判一些不必要的错误
        Preconditions.checkArgument(expenseLimit != NULL_EXPENSE || Objects.nonNull(primaryProject));

        return (expenseLimit != NULL_EXPENSE) ?
                expenseLimit :
                primaryProject.getMemberExpenseLimit();
    }

}
